package com.ai.redis.util.thread;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

public class PipelineThread<T> extends Thread {
	private static final transient Log log = LogFactory.getLog(PipelineThread.class);
	JedisPool pool = null;
	private Jedis jedis;
	private long t = 0;
	Map<String, T> vmap;  

	public PipelineThread(JedisPool pool, Map<String, T> vmap) {
		this.pool = pool;
 		jedis = pool.getResource();
		this.vmap = vmap;
	}
	
	public void run() {  
		
		if (log.isDebugEnabled()) { 
			t = System.currentTimeMillis();
			//log.debug("Thread begin. SLOT:" + jedis.getClient().getHost() + ". Data Number:" + vmap.size());
		} 
		Pipeline p = jedis.pipelined();
		for (String k : vmap.keySet()) {
			set(p, k); 
		}
		p.sync(); 

		if (log.isDebugEnabled()) { 
			log.debug(System.currentTimeMillis() - t);
			//log.debug("Thread end.   SLOT:" + jedis.getClient().getHost() + ". It took " + (System.currentTimeMillis() - t) +"ms");
		}  
	}

	protected void set(Pipeline p, String k) {
//		T t= vmap.get(k);
//		p.set(k, (String)t);
//		T t= vmap.get(k);
//		if(t instanceof String){
//			p.set(k, (String)t);
//		}
//		if(t instanceof Set){
//			Set<String> dataSet = (Set<String>)t;
//			String[] dataArray = dataSet.toArray(new String[dataSet.size()]);
//			p.sadd(k, dataArray); 
//		}
//		if(t instanceof Map){ 
//			p.hmset(k, (Map<String, String>) t);
//		}
//		if(t instanceof List){ 
//			List<String> dataList = (List<String>)t;
//			String[] dataArray = dataList.toArray(new String[dataList.size()]); 
//			p.lpush(k, dataArray);
//		}
	}
	 
	 
}